package com.example.springvue.common.handler;

import com.example.springvue.common.returnthrow.Result;
import com.example.springvue.common.returnthrow.ResultStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;


/***
 * 自定义错误抛出类 NotExistException
 * */
@ControllerAdvice
public class MyExceptionHandler {
    @ResponseBody
    @ExceptionHandler(NotExistException.class)
    public Result<Map<String, Object>> handleException(NotExistException e) {
        System.out.println(e.getMessage());
        //利用自定义的Result类
        return Result.failure(e.getMessage(), ResultStatus.INTERNAL_SERVER_ERROR);
    }

    @ResponseBody
    @ExceptionHandler(TokenNotExistException.class)
    public Result<Map<String, Object>> TokenHandleException(TokenNotExistException e) {
        System.out.println(e.getMessage());
        //利用自定义的Result类
        return Result.failure(e.getMessage(), ResultStatus.UNAUTHORIZED);
    }

}
